
package com.wyzz.hr.bo.impl;

import java.util.Date;
import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.Status;
import com.wyzz.hr.bo.IEmpHistOrgBo;
import com.wyzz.hr.bo.IEmpQuitBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.dao.IEmpQuitDao;
import com.wyzz.hr.pojo.EmpHistOrg;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empquit;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.Pager;


public class EmpQuitBoImpl implements IEmpQuitBo, Status {

	public EmpQuitBoImpl() {
	}

	public List searchByEmpNo(String empNo) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empquit.class);
		detachedCriteria.setFetchMode(Empquit.PROP_EMPLOYEE, FetchMode.JOIN);
		detachedCriteria.setFetchMode(Empquit.PROP_EQ_PERMISSION,
				FetchMode.JOIN);
		detachedCriteria.setFetchMode(Empquit.PROP_EQ_DEPT_NO, FetchMode.JOIN);
		detachedCriteria.setFetchMode(Empquit.PROP_EQ_PB_NO, FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq((new StringBuilder()).append(
				Empquit.PROP_EMPLOYEE).append(".").append(Employee.PROP_ID)
				.toString(), empNo));
		detachedCriteria.addOrder(Order.desc(Empquit.PROP_EQ_CREATE_DATE));
		List result = empQuitDao.findByCriteria(detachedCriteria);
		return result;
	}

	public List findEmpQuit(DetachedCriteria dc, Pager page,
			String searchOrExport) {
		if (page == null || "export".equals(searchOrExport)) {
			return empQuitDao.findByCriteria(dc);
		} else {
			page.splitPage(dc);
			return empQuitDao.findByCriteria(dc, page.getPageSize(), page
					.getCurrentPage());
		}
	}

	public List findQuitEmp(DetachedCriteria dc, Pager page,
			String searchOrExport) {
		if (page == null || "export".equals(searchOrExport)) {
			return empQuitDao.findByCriteria(dc);
		} else {
			page.splitPage(dc);
			return empQuitDao.findByCriteria(dc, page.getPageSize(), page
					.getCurrentPage());
		}
	}

	public Empquit getById(String quitId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empquit.class);
		detachedCriteria.setFetchMode(Empquit.PROP_EMPLOYEE, FetchMode.JOIN);
		detachedCriteria.setFetchMode(Empquit.PROP_EQ_PERMISSION,
				FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq(Empquit.PROP_EQ_ID, quitId));
		List result = empQuitDao.findByCriteria(detachedCriteria);
		if (result.size() == 0)
			return null;
		else
			return (Empquit) result.get(0);
	}

	public String saveEmpQuit(Empquit empQuit) {
		IEmployeeBo employeeBo = (IEmployeeBo) SpringBeanFactory
				.getBean("empBo");
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		IEmpHistOrgBo emphistorgBo = (IEmpHistOrgBo) SpringBeanFactory
				.getBean("empHistOrgBo");
		Employee employee = employeeBo.loadEmp(empQuit.getEmployee().getId(),
				new String[] { Employee.PROP_EMP_PB_NO });
		Integer status = Integer.valueOf(Integer.parseInt(empQuit.getEqType()));
		if (status.intValue() == 1) {
			Position pos = posBo.getPosById(empQuit.getPosition().getId(),
					new String[] { Position.PROP_POSITION_PB_ID });
			employee.setEmpStatus(Integer.valueOf(1));
			empQuit.setEqDeptNo(pos.getPositionPbId().getPbDeptId());
			empQuit.setEqPbNo(pos.getPositionPbId());
			empQuitDao.saveObject(empQuit);
			employee.setEmpTerminateDate(null);
			employee.setQuit(null);
			employee.setEmpDeptNo(pos.getPositionPbId().getPbDeptId());
			employee.setEmpPbNo(pos.getPositionPbId());
			employee.setEmpLastChangeTime(new Date());
			employee.setEmpLastChangeBy(new Employee(empQuit
					.getEqLastChangeBy()));
			empQuitDao.saveOrupdate(employee);
			String parentPosId = pos.getPositionParentId() == null ? null : pos
					.getPositionParentId().getId();
			posBo.batchSetEmpPos(parentPosId, pos.getPositionPbId().getId(),
					new String[] { employee.getId() });
			emphistorgBo.saveEmpHistOrg(employee, employee.getEmpDeptNo(),
					employee.getEmpPbNo());
		} else {
			employee.setEmpStatus(Integer.valueOf(0));
			employee.setEmpTerminateDate(empQuit.getEqDate());
			empQuit.setEqDeptNo(employee.getEmpDeptNo());
			empQuit.setEqPbNo(employee.getEmpPbNo());
			empQuitDao.saveObject(empQuit);
			employee.setQuit(empQuit);
			employee.setEmpLastChangeTime(new Date());
			employee.setEmpLastChangeBy(new Employee(empQuit
					.getEqLastChangeBy()));
			empQuitDao.saveOrupdate(employee);
			Position pos = posBo.getPosByEmpNo(employee.getId(),
					new String[] { Position.PROP_POSITION_PB_ID });
			if (pos != null) {
				pos.setPositionEmpNo(null);
				empQuitDao.saveOrupdate(pos);
			}
			EmpHistOrg latestHistOrg = emphistorgBo
					.getLatestEmphistorg(employee);
			if (latestHistOrg != null) {
				latestHistOrg.setEhoValidTo(new Date());
				latestHistOrg.setEhoIsLatest(new Integer(0));
				empQuitDao.saveOrupdate(latestHistOrg);
			}
			UserBo userBo = (UserBo) SpringBeanFactory.getBean("userService");
			userBo.updateStatus(employee.getId(), 0, empQuit
					.getEqLastChangeBy());
		}
		return "success";
	}

	public String delete(String etfId, Employee currentEmp) {
		Empquit empQuit = getById(etfId);
		IEmployeeBo employeeBo = (IEmployeeBo) SpringBeanFactory
				.getBean("empBo");
		Employee employee = employeeBo.loadEmp(empQuit.getEmployee().getId(),
				null);
		Integer empStatus = employee.getEmpStatus();
		Date empTerDate = employee.getEmpTerminateDate();
		Empquit q = null;
		String hql;
		if (Integer.parseInt(empQuit.getEqType()) == 1) {
			employee.setEmpStatus(Integer.valueOf(0));
			employee.setEmpTerminateDate(new Date());
			hql = (new StringBuilder()).append(
					"from Empquit where employee.id='")
					.append(employee.getId()).append(
							"' and eqType<>1 order by eqLastChangeTime desc")
					.toString();
			List rs = empQuitDao.exeHqlList(hql);
			if (rs.size() > 0) {
				q = (Empquit) rs.get(0);
				employee.setQuit(q);
			}
		} else {
			employee.setEmpStatus(Integer.valueOf(1));
			employee.setEmpTerminateDate(null);
			employee.setQuit(null);
		}
		if (q != null)
			employee.setEmpTerminateDate(q.getEqDate());
		employee.setEmpLastChangeTime(new Date());
		employee.setEmpLastChangeBy(currentEmp);
		empQuitDao.updateObject(employee);
		hql = (new StringBuilder()).append("delete from Empquit where eqId='")
				.append(etfId).append("'").toString();
		empQuitDao.exeHql(hql);
		return null;
	}

	public void update(Empquit empQuit, String quitId, Employee currentEmp) {
		IEmployeeBo employeeBo = (IEmployeeBo) SpringBeanFactory
				.getBean("empBo");
		Employee employee = employeeBo.loadEmp(empQuit.getEmployee().getId(),
				null);
		Integer status = Integer.valueOf(Integer.parseInt(empQuit.getEqType()));
		if (status.intValue() == 1)
			status = Integer.valueOf(1);
		else
			status = Integer.valueOf(0);
		employee.setEmpStatus(status);
		employee.setEmpTerminateDate(empQuit.getEqDate());
		employee.setEmpLastChangeBy(currentEmp);
		employee.setEmpLastChangeTime(new Date());
		Empquit oldQuit = getById(quitId);
		oldQuit.setEmployee(empQuit.getEmployee());
		oldQuit.setEqDate(empQuit.getEqDate());
		oldQuit.setEqPermission(empQuit.getEqPermission());
		oldQuit.setEqReason(empQuit.getEqReason());
		oldQuit.setEqType(empQuit.getEqType());
		oldQuit.setErComments(empQuit.getErComments());
		empQuitDao.saveOrupdate(oldQuit);
	}

	public IEmpQuitDao getEmpQuitDao() {
		return empQuitDao;
	}

	public void setEmpQuitDao(IEmpQuitDao empQuitDao) {
		this.empQuitDao = empQuitDao;
	}

	IEmpQuitDao empQuitDao;
}
